<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="合同编号" prop="vBillNo">
        <el-input v-model="queryParams.vBillNo" clearable />
      </el-form-item>
      <el-form-item label="项目号" prop="vProjectNo">
        <el-input v-model="queryParams.vProjectNo" clearable />
      </el-form-item>
      <el-form-item label="项目名称" prop="vProjectDec">
        <el-input v-model="queryParams.vProjectDec" clearable />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleInv"
          >执行入库</el-button
        >
      </el-col>
    </el-row>

    <el-table :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" prop="index" />
      <el-table-column label="物料名称" align="center" prop="vSpaName" />

      <el-table-column
        label="规格型号"
        align="center"
        prop="vSpaModel"
        width="200px"
      />
      <el-table-column label="品牌" align="center" prop="vSpaBrand" />
      <el-table-column label="数量" align="center" prop="nQty" />
      <el-table-column
        prop="nThisQty"
        label="本次操作数量"
        align="center"
        min-width="100"
      >
        <template slot-scope="scope">
          <el-input-number size="small" v-model="scope.row.nThisQty">
            <template slot="prepend">
              <el-button size="small" icon="el-icon-minus"> </el-button>
            </template>
            <template slot="append">
              <el-button size="small" icon="el-icon-plus"> </el-button
            ></template>
          </el-input-number>
          <el-button type="text" @click="toMax(scope.row)"> 最大</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="供应商"
        align="supplier"
        prop="supplier"
        width="200px"
      />
      <el-table-column prop="vWHId" label="仓库" align="center">
        <template slot-scope="scope">
          <el-select
            size="small"
            clearable
            v-model="scope.row.vWHId"
            @change="handleWHEdit(scope.row)"
          >
            <el-option
              v-for="dict in whList"
              :key="dict.id"
              :label="dict.vWHName"
              :value="dict.id"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="vAreaId" label="库区" align="center">
        <template slot-scope="scope">
          <el-select
            size="small"
            v-model="scope.row.vAreaId"
            clearable
            @change="handleAreaEdit(scope.row)"
          >
            <el-option
              v-for="dict in areaList"
              :key="dict.id"
              :label="dict.vWHName"
              :value="dict.id"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="vLocationId" label="库位" align="center">
        <template slot-scope="scope">
          <el-select
            size="small"
            v-model="scope.row.vLocationId"
            clearable
            @change="handleLocaltionEdit(scope.row)"
          >
            <el-option
              v-for="dict in localtionList"
              :key="dict.id"
              :label="dict.vWHName"
              :value="dict.id"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
  </div>
</template>

<script>
import { getBill } from "@/api/buy/buy0003";
import { addPostWithDetail, addInvWithDetail } from "@/api/buy/post";
import { listInv0001 } from "@/api/inv/inv0001";
export default {
  name: "buy0004detail",
  dicts: ["sys_common_status", "buy_post_type", "inv_arrival_status"],
  data() {
    return {
      // 遮罩层
      loading: false,

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 弹出层标题
      title: "采购物料发货",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        vBillNo: null,
        vProjectNo: null,
        vProjectDesc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      itemList: [],
      selectionList: [],
      // 弹出层标题
      titleInv: "发票",
      // 是否显示弹出层
      openInv: false,
      // 表单参数
      formInv: {},
      whList: [],
      localtionList: [],
      areaList: [],
    };
  },
  created() {
    console.log(this.$route.params);
    getBill(this.$route.params.id).then((response) => {
      this.queryParams = response.data.billPO;
      this.itemList = response.data.itemPOList;
    });

    listInv0001({ parentId: "0", whType: "L" }).then((response) => {
      this.whList = response.data;
    });
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectionList = selection;
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        nMainId: null,
        vBillNo: null,
        vPostNo: null,
        vPostType: null,
        oprBy: this.$store.state.user.name,
        status: "00",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    resetInv() {
      this.formInv = {
        id: null,
        nMainId: null,
        vInvocieNo: null,
        nAmt: null,
        oprBy: this.$store.state.user.name,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("formInv");
    },
    handleInv() {
      this.resetInv();
      this.openInv = true;
    },
    handlePost() {
      this.reset();
      this.open = true;
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          var vContractNos = this.$route.params.vBillNo;
          this.form.vContractNos = vContractNos;
          this.form.nMainId = this.$route.params.id;
          let param = {
            buyPurchasePost: this.form,
            buyPurchasePostItems: this.selectionList,
          };

          addPostWithDetail(param).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
          });
        }
      });
    },
    cancel() {
      this.open = false;
    },
    submitFormInv() {
      var vContractNos = this.$route.params.vBillNo;
      this.formInv.vContractNos = vContractNos;
      this.formInv.nMainId = this.$route.params.id;
      let param = {
        buyPurchaseInvoice: this.formInv,
        buyPurchaseInvItems: this.selectionList,
      };

      addInvWithDetail(param).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.openInv = false;
      });
    },
    cancelInv() {
      this.openInv = false;
    },
    toMax(row) {
      row.nThisQty = row.nQty;
    },

    handleWHEdit(row) {
      row.vAreaId = "";
      row.vLocationId = "";
      this.localtionList = [];
      if (row.vWHId != "" && row.vWHId != "undefined") {
        listInv0001({ parentId: row.vWHId, whType: "L" }).then((response) => {
          this.areaList = response.data;
          this.areaDisabled = false;
        });
      } else {
        this.areaList = [];
        this.areaDisabled = true;
        this.localtionDisabled = true;
      }
    },

    handleAreaEdit(row) {
      row.vAreaId = row.vAreaId;
      row.vLocationId = "";
      this.localtionList = [];
      if (row.vAreaId != "" && row.vAreaId != "undefined") {
        listInv0001({ parentId: row.vAreaId, whType: "L" }).then((response) => {
          this.localtionList = response.data;
          this.localtionDisabled = false;
        });
      } else {
        this.localtionDisabled = true;
      }
    },

    handleLocaltionEdit(row) {
      if (row.vAreaId != "" && row.vAreaId != "undefined") {
        listInv0001({ parentId: row.vAreaId, whType: "L" }).then((response) => {
          this.localtionList = response.data;
          row.vLocationId = row.vLocationId;
        });
      }
    },
  },
};
</script>
<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
